<?php
$db = new Database();
$fs = new functions();
$auth = Auth::getAuth('current_user');
$email = new Mail_Notification();
$userCompany = new userQueries();
    $company = $userCompany->getCompany($auth['company_id']);
    $action = $_POST['action'];
    
    if(isset($action)){
        
        // Edit Personal Info
        if($action=="edit_personal_info"){
            $dataID = $_POST['dataID'];
            $displayName = $_POST['displayName'];
            $firstName = $_POST['firstName'];
            $middleName = $_POST['middleName'];
            $lastName = $_POST['lastName'];
            $contactNumber = $_POST['contactNumber'];
            
                $set = array("display_name"     =>      $displayName,
                             "first_name"       =>      $firstName,
                             "middle_name"      =>      $middleName,
                             "last_name"        =>      $lastName,
                             "contact_number"   =>      $contactNumber);
            
            $condition = array("id"             =>      $dataID);
            
            $updateInfo = $db->update("tbuser",$set,$condition);
            
                $info = array("display_name"    =>      $displayName,
                             "first_name"       =>      $firstName,
                             "middle_name"      =>      $middleName,
                             "last_name"        =>      $lastName,
                             "contact_number"   =>      $contactNumber,
                             "msg"              =>      "Your information was successfully updated.");
        
            echo json_encode($info);
            
            $login = $db->query("SELECT *
                                    FROM tbuser
                                    WHERE id={$db->escape($dataID)} ","row");
            Auth::setAuth('current_user',$login);
        }
        
        // Edit Company Info
        
        elseif($action=="edit_company_info"){
            $dataID = $_POST['dataID'];
            $companyName = $_POST['companyName'];
            $companyCode = $_POST['companyCode'];
            $companyNumber = $_POST['companyNumber'];
            
                $set = array("name"                 =>      $companyName,
                             "code"                 =>      $companyCode,
                             "contact_number"       =>      $companyNumber);
                
                $condition = array("id"             =>      $dataID);
                
            $updateInfo = $db->update("tbcompany",$set,$condition);
            
                $info = array("companyName"         =>      $companyName,
                             "companyCode"          =>      $companyCode,
                             "companyNumber"        =>      $companyNumber,
                             "msg"                  =>      "Your company information was successfully updated.");
        
            echo json_encode($info);
        }
        
        elseif($action=="editUsername"){
            $oldUsername = $_POST['oldUsername'];
            $newUsername = $_POST['newUsername'];
            if(!$fs->VerifyMailAddress($newUsername)){
                echo "Please type your correct email format.";
            }else{
                if($newUsername==""){
                    echo "Input your new Email as your new Username.";
                }else{
                    // Update Table User
                    $set = array("email"=>$newUsername);
                    $condition = array("id"=>$auth['id']);
                    $db->update("tbuser",$set,$condition);
                    
                    // Update Table Company
                    $set = array("email"=>$newUsername);
                    $condition = array("id"=>$company['id']);
                    $db->update("tbcompany",$set,$condition);
                    
                    // Auth
                    $login = $db->query("SELECT *
                                            FROM tbuser
                                            WHERE id={$db->escape($auth['id'])} ","row");
                    Auth::setAuth('current_user',$login);
                    
                    echo "Your New Email was Successfully Updated.";
                }
            }
            
        }
        
        elseif($_POST['action']=="newPassword"){
            $new_password = $fs->encrypt_decrypt("encrypt",$_POST['newPassword']);
            $oldPassword = $_POST['oldPassword'];
            $myPassword = $fs->encrypt_decrypt("decrypt",$auth['password']);
            if($myPassword!=$oldPassword){
                echo "no";
            }else{
                $conditions = array("id"=>$auth['id']);
                $fields = array("password"=>$new_password);
                $db->update("tbuser",$fields,$conditions);
                
                // Auth
                $login = $db->query("SELECT *
                                        FROM tbuser
                                        WHERE id={$db->escape($auth['id'])} ","row");
                Auth::setAuth('current_user',$login);
            }
        }
        
    }
?>